package framework.rpc.protocol.http.handler;

import framework.rpc.protocol.http.transport.Invocation;
import framework.rpc.protocol.register.LocalRegister;
import framework.rpc.util.IoUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ObjectInputStream;
import java.lang.reflect.Method;

public class HttpServerHandler {
    public void handler(HttpServletRequest req, HttpServletResponse resp) {
        //处理请求逻辑
        try {
            Invocation invocation = (Invocation) new ObjectInputStream(req.getInputStream()).readObject();
            String interfaceName = invocation.getInterfaceName();
            Class classImpl = LocalRegister.get(interfaceName);
            Method method = classImpl.getMethod(invocation.getMethodName(), invocation.getPramType());
            Object invoke = method.invoke(classImpl.newInstance(), invocation.getParams());

            IoUtil.write(String.valueOf(invoke), resp.getOutputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
